/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.MainInDAO;
import DTO.MailInDTO;
import DTO.MailStructureDTO;
import DTO.TableMailDataDTO;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author NxSiHMr0D
 */
public class MailInBUS {

    MainInDAO _tmp = new MainInDAO();
    public static DefaultTreeModel model;
    public static MutableTreeNode node;

    /**
     * search folder mail by name
     * @param name
     * @return
     * @throws java.sql.SQLException
     */
    public MailInDTO searchMailIn(String name) throws SQLException {
        return _tmp.searchMailIn(name);
    }

    /**
     * get all of Folder mail
     * @return
     * @throws java.sql.SQLException
     */
    public ArrayList<MailInDTO> getListBox() throws SQLException {
        return _tmp.getListBox();
    }

    /**
     * get node on tree by name.
     * @param root
     * @param name
     * @return
     */
    public DefaultMutableTreeNode getNode(DefaultMutableTreeNode root, String name) {
        if (root.toString().equals(name)) {
            return root;
        }
        int count = root.getChildCount();
        DefaultMutableTreeNode result = null;
        for (int index = 0; index < count; index++) {
            DefaultMutableTreeNode node1 = (DefaultMutableTreeNode) root.getChildAt(index);
            result = getNode(node1, name);
        }
        return result;
    }
    // @SuppressWarnings("empty-statement")

    /**
     * show listbox to tree
     * @param trList
     * @param lstBox
     */
    public void showListBox(JTree trList, ArrayList<MailInDTO> lstBox) {

        if (lstBox.size() <= 4) {
            return;
        }
        DefaultMutableTreeNode parent = getNode((DefaultMutableTreeNode) trList.getModel().getRoot(), "User Folder");
        ;
        model = (DefaultTreeModel) trList.getModel();
        parent.removeAllChildren();
        for (int index = 4; index < lstBox.size(); index++) {
            MailInDTO tmp = lstBox.get(index);
            //DefaultMutableTreeNode tr = new DefaultMutableTreeNode(tmp.getNameIn());


            node = new DefaultMutableTreeNode(tmp.getNameIn());
            parent.add(node);


        }
        model.reload();
        for (int i = 0; i < trList.getRowCount(); i++) {
            trList.expandRow(i);
        }

    }

    /**
     * reload the tree.
     * @param trList
     */
    public void reLoad(JTree trList) {
        model = (DefaultTreeModel) trList.getModel();
        model.reload();
    }
    //@SuppressWarnings("empty-statement")

    /**
     * add node tree
     * @param trList
     * @param nodeName
     * @throws java.sql.SQLException
     */
    public void addNode(JTree trList, String nodeName) throws SQLException {
        //TreePath path = trList.getNextMatch(nodeName, startingRow, bias)
//        DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) trList
//            .getLastSelectedPathComponent();
//        if (selNode != null) {
//          DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(nodeName);
//          model.insertNodeInto(newNode, selNode, selNode.getChildCount());
//          TreeNode[] nodes = model.getPathToRoot(newNode);
//          TreePath path = new TreePath(nodes);
//          trList.scrollPathToVisible(path);
//          trList.setSelectionPath(path);
//          trList.startEditingAtPath(path);
//        }
        DefaultMutableTreeNode parent = getNode((DefaultMutableTreeNode) trList.getModel().getRoot(), "User Folder");
        ;
        model = (DefaultTreeModel) trList.getModel();
        DefaultMutableTreeNode nosde = new DefaultMutableTreeNode(nodeName);
        // parent.add(nosde);
        int index = parent.getChildCount();
        model.insertNodeInto(nosde, parent, index);
//          trList.setModel(model);
        TreeNode[] nodes = model.getPathToRoot(parent);
        TreePath path = new TreePath(nodes);
        trList.scrollPathToVisible(path);
        trList.setSelectionPath(path);
        trList.startEditingAtPath(path);
        _tmp.insertFolder(nodeName);
    }

    /**
     * delete node by name
     * @param trList
     * @param nodename
     */
    public void deleteNode(JTree trList, String nodename) {
        DefaultMutableTreeNode parent = getNode((DefaultMutableTreeNode) trList.getModel().getRoot(), "User Folder");

        model = (DefaultTreeModel) trList.getModel();
        DefaultMutableTreeNode nosde = getNode(parent, nodename);
        model.removeNodeFromParent(nosde);
        // parent.add(nosde);


//          trList.setModel(model);
        TreeNode[] nodes = model.getPathToRoot(parent);
        TreePath path = new TreePath(nodes);
        trList.scrollPathToVisible(path);
        trList.setSelectionPath(path);
        trList.startEditingAtPath(path);

    }

    /**
     * deletenode by nodetree
     * @param trList
     * @param nodename
     * @throws java.sql.SQLException
     */
    public void deleteNode(JTree trList, DefaultMutableTreeNode nodename) throws SQLException {
        DefaultMutableTreeNode parent = getNode((DefaultMutableTreeNode) trList.getModel().getRoot(), "User Folder");

        model = (DefaultTreeModel) trList.getModel();

        model.removeNodeFromParent(nodename);
        // parent.add(nosde);


//          trList.setModel(model);
        TreeNode[] nodes = model.getPathToRoot(parent);
        TreePath path = new TreePath(nodes);
        trList.scrollPathToVisible(path);
        trList.setSelectionPath(path);
        trList.startEditingAtPath(path);
        MailInDTO mailIn = _tmp.searchMailIn(nodename.toString());
        if (mailIn != null) {
            _tmp.deleteFolder(mailIn);
        }

    }

    /**
     * search node by name.
     * @param nodename
     * @return
     * @throws java.sql.SQLException
     */
    public Boolean searchNode(String nodename) throws SQLException {
        MailInDTO in = _tmp.searchMailIn(nodename);
        if (in != null) {
            return true;
        }
        return false;
    }

    /**
     * set mail to folder mail.
     * @param rows
     * @param tbListMail
     * @param IdFolder
     * @throws java.sql.SQLException
     */
    public void setFolder(int rows, JTable tbListMail, int IdFolder) throws SQLException {
        MailStructureDTO mail = ((TableMailDataDTO) tbListMail.getModel()).getRealData().get(rows);
        int IDMail = mail.getIdMail();
        _tmp.move2Folder(IDMail, IdFolder);
    //_model.setTypeMail(Mask, rows);
    }

    /**
     * modify information of Folder mail.
     * @param mofify
     * @throws java.sql.SQLException
     */
    public void modifyFolder(MailInDTO mofify) throws SQLException {
        _tmp.modifyFolder(mofify);
    }
}
